package com.jming.jike.service.backend.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jming.jike.common.BusinessException;
import com.jming.jike.common.PageResult;
import com.jming.jike.dto.backend.CategoryDTO;
import com.jming.jike.entity.Category;
import com.jming.jike.entity.Dish;
import com.jming.jike.entity.SetMeal;
import com.jming.jike.enums.CategoryTypeEnum;
import com.jming.jike.mapper.CategoryMapper;
import com.jming.jike.mapper.DishMapper;
import com.jming.jike.mapper.SetMealMapper;
import com.jming.jike.service.backend.CategoryService;
import com.jming.jike.util.CopyBeanUtils;
import com.jming.jike.vo.backend.CategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

import static com.jming.jike.enums.CategoryTypeEnum.DISHES_TYPE;
import static com.jming.jike.enums.CategoryTypeEnum.SET_MEAL_TYPE;

/**
 * @author jinming.zhang
 * @version 0.0.1
 * @date 2022/04/16
 */
@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private DishMapper dishMapper;

    @Resource
    private SetMealMapper setMealMapper;

    @Override
    public Boolean insert(CategoryDTO categoryDTO) {

        categoryDTO.setId(null);
        log.info("CategoryServiceImpl insert 添加菜品分类信息 入参 {}", JSON.toJSONString(categoryDTO));

        // 添加并返回结果
        return categoryMapper.insert(CopyBeanUtils.copyBean(categoryDTO, new Category())) > 0;
    }

    @Override
    public Boolean update(CategoryDTO categoryDTO) {

        log.info("CategoryServiceImpl insert 修改菜品分类信息 入参 {}", JSON.toJSONString(categoryDTO));

        // 修改并返回结果
        return categoryMapper.updateById(CopyBeanUtils.copyBean(categoryDTO, new Category())) > 0;
    }

    @Override
    public Boolean delete(Long id) {

        log.info("CategoryServiceImpl insert 删除分类信息 入参 {}", id);

        // 查询是否存在
        Category category = categoryMapper.selectById(id);
        if (ObjectUtils.isEmpty(category)) {
            throw new BusinessException("菜品分类不存在!");
        }

        // 查询是否绑定菜品或者套餐信息
        if (DISHES_TYPE.getCode().equals(category.getType())) {
            if (!ObjectUtils.isEmpty(dishMapper.selectOne(Wrappers.<Dish>lambdaQuery()
                    .eq(Dish::getIsDeleted, 0)
                    .eq(Dish::getCategoryId, id).last("limit 1")))) {

                throw new BusinessException("该分类绑定了菜品, 不可删除!");
            }
        } else if (SET_MEAL_TYPE.getCode().equals(category.getType())) {
            if (!ObjectUtils.isEmpty(setMealMapper.selectOne(Wrappers.<SetMeal>lambdaQuery()
                    .eq(SetMeal::getIsDeleted, 0)
                    .eq(SetMeal::getCategoryId, id).last("limit 1")))) {

                throw new BusinessException("该分类绑定了套餐, 不可删除!");
            }
        }

        // 删除并返回结果
        return categoryMapper.deleteById(id) > 0;
    }

    @Override
    public PageResult<CategoryVO> pageQuery(CategoryDTO categoryDTO) {

        log.info("CategoryServiceImpl pageQuery 分页查询分类信息 入参 {}", JSON.toJSONString(categoryDTO));

        // 根据条件查询
        Page<Category> categoryPage = categoryMapper.selectPage(new Page<>(categoryDTO.getPageIndex(), categoryDTO.getPageSize()),
                Wrappers.<Category>lambdaQuery().orderByDesc(Category::getId));

        // 如果为空 抛出异常
        if (ObjectUtils.isEmpty(categoryPage) || CollectionUtils.isEmpty(categoryPage.getRecords())) {
            throw new BusinessException("分类列表为空!");
        }

        // 处理数据
        List<CategoryVO> categoryVOList = CopyBeanUtils.copyBeanList(categoryPage.getRecords(), CategoryVO.class);

        // 返回分页对象
        return new PageResult<>((int) categoryPage.getCurrent(), (int) categoryPage.getSize(),
                (int) categoryPage.getTotal(), categoryVOList);
    }

    @Override
    public CategoryVO queryDetail(Long id) {

        log.info("CategoryServiceImpl pageQuery 根据id查询分类详情信息 入参 {}", id);

        // 查询并返回结果
        return CopyBeanUtils.copyBean(categoryMapper.selectById(id), new CategoryVO());
    }

    @Override
    public List<CategoryVO> queryCategoryListByType(Integer type) {

        // 检查入参
        CategoryTypeEnum.checkCategoryByType(type);

        // 查询并返回结果
        return CopyBeanUtils.copyBeanList(categoryMapper.selectList(Wrappers.<Category>lambdaQuery()
                .eq(Category::getType, type)
                .orderByAsc(Category::getSort)
                .orderByAsc(Category::getId)), CategoryVO.class);
    }

}
